import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/entities/purchase_order_entity.dart';
import 'package:library_base/entities/suppliers_shops_list_entity.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:module_administrator/page/purchase_payment/purchase_payment_detail/purchase_payment_detail_controller.dart';

class PurchasePaymentDetailPage extends StatefulWidget {
  const PurchasePaymentDetailPage({Key? key}) : super(key: key);

  @override
  _PurchasePaymentDetailPageState createState() => _PurchasePaymentDetailPageState();
}

class _PurchasePaymentDetailPageState extends BaseState<PurchasePaymentDetailPage,PurchasePaymentDetailController> {
  @override
  PurchasePaymentDetailController? registerController() => Get.find<PurchasePaymentDetailController>();

  late PurchaseOrderEntity purchaseOrderEntity;

  @override
  void onCreate() {
    super.onCreate();
    setTitle(MultiLanguage.purchaseOrderQuotation.tr);
    setNavigationBarColor(ThemeUtil.getTheme().secondaryBgColor(),);
    purchaseOrderEntity = Get.arguments['purchaseOrderEntity'];
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return Container(
      color: ThemeUtil.getTheme().secondaryBgColor(),
      child: Column(children: [
          Container(padding: edgeLR24,
          color: ThemeUtil.getTheme().colorScheme.secondary,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              gapH24,
              Row(children: [
                Text(MultiLanguage.purchaseOrderNo.tr+" : ",style: normalTextColor28,),
                Expanded(child: Text(purchaseOrderEntity.purchaseOrderNumber,style: normalTextColor28)),
              ]),
              gapH24,
              Row(children: [
                Text(MultiLanguage.commodityQuantity.tr+" : ",style: normalTextColor28,),
                Expanded(child: Text(purchaseOrderEntity.purchaseCount.toString(),style: normalTextColor28)),
              ]),
              gapH24,
              Row(children: [
                Text("${MultiLanguage.remark.tr}: ",style: normalTextColor28,),
                Expanded(child: Text(purchaseOrderEntity.remark??"",style: normalTextColor28)),
              ]),
              purchaseOrderEntity.shop!=null ? gapH24:Container(),
              purchaseOrderEntity.shop!=null ? Row(children: [
                Text("${MultiLanguage.salesOrder.tr}: ",style: normalTextColor28,),
                Expanded(child: Text("${MultiLanguage.purchasePrice.tr}${getPriceModelStr(purchaseOrderEntity.shop!)}",style: normalTextColor28)),
              ]):Container(),
              gapH24,
            ],
          ),
          ),
          gapH18,
        Container(
          color: ThemeUtil.getTheme().colorScheme.secondary,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: _buildForm(),
          ),
        ),
      ]),
    );
  }

  List<Widget> _buildForm(){
    List<Widget> widgets = [];
    widgets.add(Container(
      padding: edgeV24,
      child: Row(children: [
        Expanded(child: Text(MultiLanguage.productName.tr,style: sixthTextColor28,textAlign: TextAlign.center)),
        Expanded(child: Text("${MultiLanguage.purchaseWeight.tr}\n${SPUtil.isJin.unitStrZhWithBracket()}",style: sixthTextColor28,textAlign: TextAlign.center)),
        Expanded(child: Text(MultiLanguage.purchasePrices.tr,style: sixthTextColor28,textAlign: TextAlign.center)),
        Expanded(child: Text(MultiLanguage.serviceCharge.tr,style: sixthTextColor28,textAlign: TextAlign.center)),
        Expanded(child: Text(MultiLanguage.subtotal.tr,style: sixthTextColor28,textAlign: TextAlign.center)),
      ]),
    ));
    widgets.add(line1);
    double totalPrice = 0.0;
    widgets.addAll(purchaseOrderEntity.details.buildItem(
            (index,data){
              totalPrice = NumUtil.add(totalPrice, data.totalCost);
              return Container(
                padding: edgeV24,
                child: Row(
                  children: [
                    Expanded(child: Text(data.goodsName,style: normalTextColor28,textAlign: TextAlign.center)),
                    Expanded(child: Text(data.purchaseWeight.remoteUnitTransitionLocalUnitStr(data.unit),style: normalTextColor28,textAlign: TextAlign.center)),
                    Expanded(child: Text(data.price.remoteAsLocalUnit(data.unit).toString(),style: normalTextColor28,textAlign: TextAlign.center)),
                    Expanded(child: Text(data.serviceCost.toString(),style: normalTextColor28,textAlign: TextAlign.center)),
                    Expanded(child: Text(data.totalCost.toString(),style: normalTextColor28,textAlign: TextAlign.center)),
                  ],
                ),
              );
              },
        dividingLine: (){
          return line1;
        },divideType: DivideType.MIDDLE_WITH_BOTTOM
    ));
    widgets.add(Container(
      padding: edgeV24,
      child: Row(children: [
        Expanded(child: Text(MultiLanguage.total.tr,style: normalTextColor28_1,textAlign: TextAlign.center)),
        Expanded(flex:3,child: Container()),
        Expanded(child: Text(purchaseOrderEntity.totalCost.toString(),style: normalTextColor28_1,textAlign: TextAlign.center)),
      ]),
    ));
    return widgets;
  }

  ///返回计价模式字符串
  String getPriceModelStr(SuppliersShopsListEntity suppliersShopsListEntity){
    if(suppliersShopsListEntity.priceModel == "FIXED"){
      return "+${suppliersShopsListEntity.amount.remoteAsLocalUnit(ServiceUnit.KG.name)}${MultiLanguage.yuan.tr}/${SPUtil.isJin.unitStrZh()}";
    }else if(suppliersShopsListEntity.priceModel == "RATIO"){
      return "+${suppliersShopsListEntity.amount.toStringAsFixed(2)}%";
    }
    return "";
  }

}
